<?php

class Kit_Controller extends WakEngineController {

    function _setUp() {
        $this->id=$this->response->shiftNode();
        // kit analysis
        $this->response->kit=new WakKitFolder($this->id);
        $this->response->kit->setLocale($this->response->lang);
        // setting breadcrumb links
        $this->response->addBreadCrumb('Kits','kits');
        $kitpath='';
        foreach (explode('.',$this->id) as $part) {
            $kitpath.=$part.'.';
            $this->response->addBreadCrumb($part,'kit/'.substr($kitpath,0,-1));
        }
        // diagnostic
        /*if (!$this->response->kit->isValid()) {
            $this->_setAction('badkit');
        }*/
    }

    /**
     * kit home/index
     */
    public function index() {
        if ($this->response->request->isPost()) {
            $this->response->addfile = new WakAddFileForm($_POST);
            // echo '<pre>'; print_r($this->response->addfile);
        } else {
            $this->response->addfile = new WakAddFileForm();
        }
    }

    /**
     * adding a file
     */
    public function addfile() {
        $form = new WakAddFileForm($_POST);
        if ($form->isValid()) {
            // move uploaded file
            $php_file = $form->cleaned_data['php5']; //print_r($file);
            $name = $_FILES['php5']['name']; //echo $name;
            if (!$php_file->move($this->response->kit->getPath(), $name)) {
                throw new WakEngineErrorPage('failed to move file', 500);
            }
            // preparing to edit wak.ini
            $ini_file = $this->response->kit->getIniFile();
            $data = $ini_file->getData();
            if ($form->cleaned_data['force_load']) {
                // add to load_files
                $data['load_files'][] = $php_file->basename();
            } else {
                // add to [load_classes]
                // parse PHP file to list classes
                $classes = $php_file->definedClasses();
                foreach ($classes as $class) {
                    // adding class to load_classes
                    $data['load_classes'][$class] = $php_file->basename();
                }
                // echo '<pre>'; print_r($data);  exit;
            }
            // saving ini file
            if (!$ini_file->setData($data)) {
                throw new WakEngineErrorPage("failed to write ini file", 500);
            }
        }
        $this->kit->_redirect($this->response->request->getBaseHref()
            .'kit/'.$this->response->kit->getId());


        //$this->response->addfile = $form;
        //$this->response->setView('kit.index');
    }

    /**
    * an invalid kit page
    */
    public function badkit() {
        return true;
    }/**/

    /**
     * unit tests details
     */
    function tests() {
        // nav
        $this->response->addBreadCrumb('Tests','kit/'.$this->id.'/tests');

        // running tests
        if ($this->response->kit->hasTests())
            $this->response->tests = $this->response->kit->getTestsReport();

    }

    /**
     * edit kit's config
     */
    function cfg() {
        //$this->response->view='kit.cfg';
        $this->response->addBreadCrumb('wak.ini','kit/'.$this->id.'/cfg');
        // save sent data
        if ($this->_isPost()) {
            $this->response->saved=
                $this->response->kit->setIni($_POST['ini']);
        }
        $this->response->cfg=$this->response->kit->getIni();
        return true;
    }

    /**
     * edit a kit's tool config
     */
    function toolCfg($name) {
        // Webappkit::loadKit('webappkit.fs.core');
        $this->response->name=$name;
        $this->response->addBreadCrumb($name.'.tool.ini','kit/'.$this->id.'/toolcfg/'.$name);
        if ($this->_isPost()) {
            // saving data
            $this->response->saved=
                $this->response->kit->setToolIni($name,$_POST['ini']);
        }
        $this->response->toolcfg=$this->response->kit->getToolIni($name);
    }

    /**
     * browse / show doc pages
     */
    public function docs($id=null) {
        $this->response->doc=$this->response->kit->getDocPage($id);
    }
}
